package com.juzi.jdk8newdate;

public class Introduce {
    public static void main(String[] args) {
        System.out.println("=======================================\n" +
                "概述\n" +
                "   从Java 8开始，java.time包提供了新的日期和时间API，主要涉及的类型有：\n" +
                "                   -            LocalDate：不包含具体时间的日期。\n" +
                "                  |             LocalTime：不含日期的时间。\n" +
                "                 |              LocalDateTime：包含了日期及时间。\n" +
                "   JDK8新增日期类 <               Instant：代表的是时间戳。\n" +
                "                 |              DateTimeFormatter 用于做时间的格式化和解析的\n" +
                "                  |             Duration:用于计算两个“时间”间隔 \n" +
                "                   -            Period:用于计算两个“日期”间隔\n" +
                "\n" +
                "   新增的API严格区分了时刻、本地日期、本地时间，并且，对日期和时间进行运算更加方便。\n" +
                "   其次，新API的类型几乎全部是不变类型（和String的使用类似），可以放心使用不必担心被修改。\n" +
                "--------------------------------\n" +
                "LocalDate、LocalTime、LocalDateTime\n" +
                "   他们 分别表示日期，时间，日期时间对象，他们的类的实例是不可变的对象。\n" +
                "   他们三者构建对象和API都是通用的\n" +
                "--------------------------------\n" +
                "构建对象的方式如下：\n" +
                "方法名                              说明\t\n" +
                "public static Xxxx now();  静态方法，根据当前时间创建对象        LocaDate localDate = LocalDate.now();\n" +
                "                                                           LocalTime llocalTime = LocalTime.now();\n" +
                "                                                           LocalDateTime localDateTime = LocalDateTime.now();\n" +
                "public static Xxxx of(…);  静态方法，指定日期/时间创建对象       LocalDate localDate1 = LocalDate.of(2099 , 11,11);\n" +
                "                                                           LocalTime localTime1 = LocalTime.of(11, 11, 11);\n" +
                "                                                           LocalDateTime localDateTime1 = LocalDateTime.of(2020, 10, 6, 13, 23, 43);\n" +
                "\n" +
                "--------------------------------\n" +
                "LocalDate、LocalTime、LocalDateTime获取信息的API.\n" +
                "       方法名                            说明\n" +
                "public int geYear()                获取年\n" +
                "public int getMonthValue()         获取月份（1-12）\n" +
                "Public int getDayOfMonth()         获取月中第几天乘法\n" +
                "Public int getDayOfYear()          获取年中第几天\n" +
                "Public DayOfWeek getDayOfWeek()    获取星期\n" +
                "\n" +
                "--------------------------------\n" +
                "LocalDateTime的转换API\n" +
                "               方法名                            说明\n" +
                "public LocalDate toLocalDate()         转换成一个LocalDate对象\n" +
                "public LocalTime toLocalTime()         转换成一个LocalTime对象\n" +
                "--------------------------------\n" +
                "修改相关的API\n" +
                "LocalDateTime 综合了 LocalDate 和 LocalTime 里面的方法，所以下面只用 LocalDate 和 LocalTime 来举例。\n" +
                "这些方法返回的是一个新的实例引用，因为LocalDateTime 、LocalDate 、LocalTime 都是不可变的。 \n" +
                "\n" +
                "                  方法名                                                  说明\n" +
                "plusDays, plusWeeks, plusMonths, plusYears             向当前 LocalDate 对象添加几天、几周、几个月、几年\n" +
                "minusDays, minusWeeks, minusMonths, minusYears         从当前 LocalDate 对象减去几天、几周、几个月、几年 \n" +
                "withDayOfMonth, withDayOfYear, withMonth, withYear     将月份天数、年份天数、月份、年份修改为指定的值并返回新的LocalDate对象 \n" +
                "isBefore, isAfter                                      比较两个 LocalDate \n" +
                "\n" +
                "--------------------------------\n" +
                "Instant时间戳\n" +
                "JDK8获取时间戳特别简单，且功能更丰富。Instant类由一个静态的工厂方法now()可以返回当前时间戳。\n" +
                "\n" +
                "       Instant instant = Instant.now();\n" +
                "       System.out.println(\"当前时间戳是：\" + instant);\n" +
                "\n" +
                "       Date date = Date.from(instant);\n" +
                "       System.out.println(\"当前时间戳是：\" + date);\n" +
                "\n" +
                "       instant = date.toInstant();\n" +
                "       System.out.println(instant);\n" +
                "\n" +
                "时间戳是包含日期和时间的，与java.util.Date很类似，事实上Instant就是类似JDK8 以前的Date。\n" +
                "Instant和Date这两个类可以进行转换。\n" +
                "--------------------------------\n" +
                "DateTimeFormatter\n" +
                "在JDK8中，引入了一个全新的日期与时间格式器DateTimeFormatter。\n" +
                "正反都能调用format方法。\n" +
                "\n" +
                "LocalDateTime ldt = LocalDateTime.now();\n" +
                "System.out.println(ldt);//2021-03-01T15:09:17.444190900\n" +
                "\n" +
                "DateTimeFormatter dtf = DateTimeFormatter.ofPattern(\"yyyy-MM-dd HH:mm:ss\");\n" +
                "String ldtStr = ldt.format(dtf);\n" +
                "System.out.println(ldtStr);//2021-03-01 15:09:17\u000B\n" +
                "String ldtStr1 = dtf.format(ldt);\n" +
                "System.out.println(ldtStr1);//2021-03-01 15:09:17\n" +
                "--------------------------------\n" +
                "Period\n" +
                "在Java8中，我们可以使用以下类来计算日期间隔差异：java.time.Period\n" +
                "主要是 Period 类方法 getYears()，getMonths() 和 getDays() 来计算,只能精确到年月日。\n" +
                "用于 LocalDate 之间的比较。\n" +
                "\n" +
                "LocalDate today = LocalDate.now();\n" +
                "System.out.println(today);     // 2021-03-01\n" +
                "LocalDate birthDate = LocalDate.of(1995, 1, 11);\n" +
                "System.out.println(birthDate); // 1995-01-11\n" +
                "Period period = Period.between(birthDate, today);\n" +
                "System.out.printf(\"年龄 : %d 年 %d 月 %d 日\", period.getYears(), period.getMonths(), period.getDays());\n" +
                "--------------------------------\n" +
                "Duration\n" +
                "在Java8中，我们可以使用以下类来计算时间间隔差异：java.time.Duration\n" +
                "提供了使用基于时间的值测量时间量的方法。\n" +
                "用于 LocalDateTime 之间的比较。也可用于 Instant 之间的比较。\n" +
                "\n" +
                "LocalDateTime today = LocalDateTime.now();\n" +
                "System.out.println(today);\n" +
                "LocalDateTime birthDate = LocalDateTime.of(1990,10,1,10,50,30);\n" +
                "System.out.println(birthDate);\n" +
                "\n" +
                "Duration duration = Duration.between(birthDate, today);//第二个参数减第一个参数\n" +
                "System.out.println(duration.toDays());//两个时间差的天数\n" +
                "System.out.println(duration.toHours());//两个时间差的小时数\n" +
                "System.out.println(duration.toMinutes());//两个时间差的分钟数\n" +
                "System.out.println(duration.toMillis());//两个时间差的毫秒数\n" +
                "System.out.println(duration.toNanos());//两个时间差的纳秒数\n" +
                "--------------------------------\n" +
                "1、 Duration:  用于计算两个“时间”间隔。 \n" +
                "2、 Period:      用于计算两个“日期”间隔。\n" +
                "--------------------------------\n" +
                "java.time.temporal.ChronoUnit\n" +
                "ChronoUnit类可用于在单个时间单位内测量一段时间，这个工具类是最全的了，可以用于比较所有的时间单位\n" +
                "\n" +
                "LocalDateTime today = LocalDateTime.now();\n" +
                "System.out.println(today);\n" +
                "LocalDateTime birthDate = LocalDateTime.of(1990,10,1,10,50,30);\n" +
                "System.out.println(birthDate);\n" +
                "\n" +
                "System.out.println(\"相差的年数：\" + ChronoUnit.YEARS.between(birthDate, today));\n" +
                "System.out.println(\"相差的月数：\" + ChronoUnit.MONTHS.between(birthDate, today));\n" +
                "System.out.println(\"相差的周数：\" + ChronoUnit.WEEKS.between(birthDate, today));\n" +
                "System.out.println(\"相差的天数：\" + ChronoUnit.DAYS.between(birthDate, today));\n" +
                "System.out.println(\"相差的时数：\" + ChronoUnit.HOURS.between(birthDate, today));\n" +
                "System.out.println(\"相差的分数：\" + ChronoUnit.MINUTES.between(birthDate, today));\n" +
                "System.out.println(\"相差的秒数：\" + ChronoUnit.SECONDS.between(birthDate, today));\n" +
                "System.out.println(\"相差的毫秒数：\" + ChronoUnit.MILLIS.between(birthDate, today));\n" +
                "System.out.println(\"相差的微秒数：\" + ChronoUnit.MICROS.between(birthDate, today));\n" +
                "System.out.println(\"相差的纳秒数：\" + ChronoUnit.NANOS.between(birthDate, today));\n" +
                "\n" +
                "System.out.println(\"相差的半天数：\" + ChronoUnit.HALF_DAYS.between(birthDate, today));\n" +
                "System.out.println(\"相差的十年数：\" + ChronoUnit.DECADES.between(birthDate, today));\n" +
                "System.out.println(\"相差的世纪（百年）数：\" + ChronoUnit.CENTURIES.between(birthDate, today));\n" +
                "System.out.println(\"相差的千年数：\" + ChronoUnit.MILLENNIA.between(birthDate, today));\n" +
                "System.out.println(\"相差的纪元数：\" + ChronoUnit.ERAS.between(birthDate, today));\n" +
                "");
    }
}
